{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "039f7ce8-913d-4a46-a055-f2c07738d2c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json  #处理JSON文件\n",
    "from datatime import datatime  #提供日期时间的处理功能\n",
    "\n",
    "#加载账单数据\n",
    "def bills_list(file_path):\n",
    "    try:\n",
    "        with open(file_path,'r') as file:\n",
    "            return json.load(file)  #读取JSON格式的文件内容\n",
    "    #如果文件不存在, 则抛出异常\n",
    "    except FileNotFoundError:\n",
    "        return[]\n",
    "\n",
    "#读取账单数据\n",
    "def save_bills(bills,file_path):\n",
    "    with open(file_path, 'w')as file:\n",
    "        json.dump(bills, file, indent=4)\n",
    "        #dump将数据转换为JSON格式, indent使JSON具有缩进\n",
    "\n",
    "#添加收入和支出函数\n",
    "def income(bills, data, money, category, tips):\n",
    "    if amount <=0:\n",
    "        print(\"收入金额必须为正数\")\n",
    "        return\n",
    "    bills.append({\n",
    "        'type':'income',\n",
    "        'data':data.isoformat(), \n",
    "        #date.isoformat()用于将datetime对象格式化为ISO 8601格式的字符串\n",
    "        'money':money,\n",
    "        'category':category,\n",
    "        'tips':tips\n",
    "    })\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
